package com.czxy.controller;

import com.czxy.pojo.Address;
import com.czxy.service.AddressService;
import com.czxy.vo.AddressRequest;
import com.czxy.vo.AddressResult;
import com.czxy.vo.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 张青卯
 */
@RestController
@RequestMapping
public class AddressController {

    private final HttpServletRequest servletRequest;

    private final AddressService addressService;

    @Autowired
    public AddressController(AddressService addressService, HttpServletRequest servletRequest) {
        this.addressService = addressService;
        this.servletRequest = servletRequest;
    }

    @GetMapping("/address")
    public ResponseEntity<List<Address>> getTheAddress(){
        List<Address> list = addressService.getTheAddress(servletRequest);
        return ResponseEntity.ok(list);
    }

    @PostMapping("/address")
    public ResponseEntity<AddressResult> addTheAddress(@RequestBody AddressRequest request){
        Integer id = addressService.addTheAddress(request,servletRequest);
        AddressResult result = new AddressResult(0, "成功", null, id);
        return ResponseEntity.ok(result);
    }

    @PutMapping("/address")
    public ResponseEntity<BaseResult> refreshAddress(@RequestBody AddressRequest request){
        addressService.refreshAddress(request);
        BaseResult result = new BaseResult(0, "成功");
        return ResponseEntity.ok(result);
    }
}
